package com.czn.bookshop.web;

import com.czn.bookshop.entity.Address;
import com.czn.bookshop.entity.User;
import com.czn.bookshop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 收货地址控制器
 *
 * @author ChenZeNan
 * @create 2020-12-30 17:12
 */
@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @RequestMapping("/save")
    @ResponseBody
    public String saveAddress(Address address, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        address.setUserId(user.getId());
        if (address.getIsDefault() != null && "on".equals(address.getIsDefault())) {
            address.setIsDefault("1"); //默认地址设置为1
        } else {
            address.setIsDefault("0"); //默认地址设置为0
        }
        addressService.save(address);
        return "success";
    }
}
